package com.example.coursedesign.ui.notifications;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.coursedesign.R;
import com.example.coursedesign.dao.UserDao;
import com.example.coursedesign.entity.User;

public class UserProfileActivity extends AppCompatActivity {

    private static final String TAG = "UserProfileActivity";
    private User currentUser;
    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_profile); // 确保这是新的布局文件名

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        userDao = new UserDao();
        String fixedAccount = "user123"; // 替换为实际的固定账号
        new LoadUserTask().execute(fixedAccount);
    }

    private class LoadUserTask extends AsyncTask<String, Void, User> {

        @Override
        protected User doInBackground(String... params) {
            String account = params.length > 0 ? params[0] : null;
            if (account != null) {
                return userDao.findUserByAccount(account);
            }
            return null;
        }

        @Override
        protected void onPostExecute(User user) {
            super.onPostExecute(user);
            currentUser = user;

            if (currentUser != null) {
                Log.d(TAG, "Loaded user successfully: " + currentUser.getAccount());
                updateUI(currentUser);
            } else {
                Log.e(TAG, "Failed to load user for account: 123");
                Toast.makeText(UserProfileActivity.this, "未找到固定账号的用户信息", Toast.LENGTH_SHORT).show();
                finish();
            }

            setClickListeners();
        }
    }

    private void setClickListeners() {
        Button btnEdit = findViewById(R.id.btn_edit);
        btnEdit.setOnClickListener(v -> {
            Intent intent = new Intent(UserProfileActivity.this, EditProfileActivity.class);
            intent.putExtra("user", currentUser);
            startActivityForResult(intent, 1);
        });

        Button btnQuit = findViewById(R.id.btn_quit);
        btnQuit.setOnClickListener(v -> {
            Toast.makeText(UserProfileActivity.this, "退出登录", Toast.LENGTH_SHORT).show();
            finish();
        });

        ImageView ivTouxiang = findViewById(R.id.iv_touxiang);
        ivTouxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理头像点击事件，例如选择图片
                Toast.makeText(UserProfileActivity.this, "点击了头像", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            currentUser = data.getParcelableExtra("updatedUser");
            updateUI(currentUser);
        }
    }

    private void updateUI(User user) {
        TextView tvUsername = findViewById(R.id.tv_username);
        TextView tvNickname = findViewById(R.id.et_nickname);
        TextView tvGender = findViewById(R.id.et_gender);
        TextView tvTel = findViewById(R.id.et_tel);
        ImageView ivTouxiang = findViewById(R.id.iv_touxiang);

        tvUsername.setText(user.getName());
        tvNickname.setText(user.getName());
        tvGender.setText(user.getGender());
        tvTel.setText(user.getPhone());

        if (user.getAvatar() != null && !user.getAvatar().isEmpty()) {
            byte[] decodedBytes = Base64.decode(user.getAvatar(), Base64.DEFAULT);
            Glide.with(this)
                    .load(R.drawable.a)
                    .apply(RequestOptions.circleCropTransform())
                    .into(ivTouxiang);
        } else {
            // 使用默认圆形头像
            Glide.with(this)
                    .load(R.drawable.a) // 假设你有一个默认头像资源
                    .apply(RequestOptions.circleCropTransform())
                    .into(ivTouxiang);
        }

        Toast.makeText(this, "用户信息已更新", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}